<template>
	<view>
		<uni-popup ref="popup" border-radius="10px 10px 10px 10px">
			<view class="gl-popup-view">
				<view class="gl-popup-title">
					<view class=""> 上传物流运单 </view>
					<uni-icons class="gl-button-mini gl-button-error" type="closeempty" size="18" color="#fff"
						@tap="popEventClose"></uni-icons>
				</view>
				<view class="gl-popup-body" style="display: flex;gap: 5px;flex-direction: column;">
					<uni-data-checkbox v-model="carry.act" @change="popActChange"
						:localdata="[{value:'email',text:'物流快递'},{value:'local',text:'线下自提'}]"></uni-data-checkbox>

					<view v-if="carry.act === 'email'" style="display: flex;flex-direction: column;gap: 5px;">
						<uni-data-select class="gl-data-select" v-model="carry.key" :localdata="deliList" emptyTips="没有"
							label="物流公司" placeholder="选择物流公司" style="width: 250px;"
							@change="popCarryChange"></uni-data-select>

						<view class="" style="display: flex;align-items: center;">
							<view class="gl-label-text">物流单号：</view>
							<uni-easyinput v-model="carry.card" placeholder="请输入物流单号" />
						</view>

						<uni-data-checkbox multiple v-model="carry.allin" @change="popCheckChange"
							:localdata="[{value:'split',text:'分包装箱'}]"></uni-data-checkbox>

						<scroll-view v-if="carry.allin.length > 0" scroll-y="true" style="max-height: 40vh;">
							<checkbox-group v-model="carry.goods" @change="popGoodsChange">
								<template v-for="(item, index) in curData.order_product" :key="index">
									<view v-if="item.status === 1"
										style="display: flex;align-items: center;gap: 10px;justify-content: space-between;">
										<image :src="sConf.cos + item.ext_product.face" mode=""
											style="width: 64px;height: 80px;border-radius: 5px;"></image>
										<text style="font-weight: bold;">{{item.ext_product.name}}</text>
										<text>* {{item.num}}</text>
										<label>
											<checkbox :value="item.id.toString()" />
										</label>
									</view>
								</template>
							</checkbox-group>
						</scroll-view>
					</view>
				</view>
				<view class="gl-popup-foot" style="display: flex;justify-content: flex-end;gap: 10px;">
					<view class="gl-button gl-button-base" @tap="popEventSubmit">
						<uni-icons type="checkmarkempty" size="18" color="#fff"></uni-icons>
						<text style="font-size: small;">保存</text>
					</view>
					<view class="gl-button gl-button-warning" @tap="popEventClose">
						<uni-icons type="closeempty" size="18" color="#fff"></uni-icons>
						<text style="font-size: small;">取消</text>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import psMethod from '@/common/psMethod';
	export default {
		data() {
			return {
				sConf: getApp().globalData,
				curData: null, // 当前选择的数据对象
				deliList: [], // 物流公司列表				
				carry: {
					act: 'email', // 默认物流方式
					name: null, // 物流公司名称
					key: null, // 物流公司ID					
					card: null, // 物流卡号
					allin: [], // 是否分零
					goods: [], // 装箱清单
				}
			}
		},
		mounted() {
			this.loadDeliveryList()
		},
		methods: {
			open(e) {
				this.curData = e
				this.$refs.popup.open()
			},
			/**
			 * 加载物流列表
			 */
			async loadDeliveryList() {
				let host = this.sConf.host + '/api/carry/';
				let token = this.sConf.auth.token || '';
				let res = await psMethod.authenticatorPostSync(host, token)
				console.log('加载快递公司', res);
				if (res.rescode === 200) {
					let tmpData = []
					res.resmsg.forEach(item => {
						tmpData.push({
							value: item.delivery_id,
							text: item.delivery_name
						})
					})
					this.deliList = tmpData;
				}
			},
			popEventClose() {
				this.$refs.popup.close()
				this.curData = null
				this.carry = {
					act: 'email', // 默认物流方式
					name: null, // 物流公司名称
					key: null, // 物流公司ID					
					card: null, // 物流卡号
					allin: [], // 是否分零
					goods: [], // 装箱清单
				}
			},
			popActChange(e) {
				console.log('popActChange', e);
			},
			popCarryChange(e) {
				console.log('popCarryChange', e);
				this.carry.name = this.deliList.find(item => item.value === e).text
			},
			popCheckChange(e) {
				console.log('popCheckChange', e.detail.value);
			},
			popGoodsChange(e) {
				console.log('popGoodsChange', e.detail.value);
				let tmpData = []
				e.detail.value.forEach(item => {
					tmpData.push({
						rid: item,
						num: 1
					})
				})
				this.carry.goods = tmpData
				console.log(this.carry);
			},
			popEventSubmit() {
				console.log('popEventSubmit');
				if (this.carry.act === 'local') {
					let data = {
						reqsn: this.curData.reqsn,
						local: 1
					}
					this.loadCarryPost(data)
				} else {
					if (!this.carry.key || !this.carry.card) {
						uni.showToast({
							title: '缺少物流信息'
						})
						return
					}
					if (this.carry.allin.includes('split') && this.carry.goods.length < 1) {
						uni.showToast({
							title: '缺少装箱清单'
						})
						return
					}
					let data = {
						reqsn: this.curData.reqsn,
						childs: this.carry.goods,
						name: this.carry.name,
						express: this.carry.key,
						tracking: this.carry.card,
						local: this.carry.act === 'local' ? 1 : 0,
						allin: this.carry.allin.includes('split') ? 0 : 1
					}
					this.loadCarryPost(data)
				}
			},
			loadCarryPost(data) {
				console.log('loadCarryPost', data);
				let host = this.sConf.host + '/admin/offLine/';
				let token = this.sConf.auth.token || '';
				psMethod.authenticatorPostSync(host, token, data).then(res => {
					console.log('创建物流单', res);
					let that = this
					uni.showToast({
						title: '提交成功',
						success() {							
							that.acceptEvent()
						}
					})

				}).catch(err => {
					console.log('创建物流单_失败', err);
					uni.showToast({
						title:'创建失败'
					})
				})
			},
			acceptEvent(){
				this.popEventClose()
				this.$emit('accept')
			}
		}
	}
</script>

<style lang="scss" scoped></style>